-- الٱملاء الصوتي من google. يدعم اللغة العربية و الأنجليزية في نفس الوقت
-- يجب منح الجيشو ٱذن الميكروفون

-- Import necessary AndroLua classes for Android interaction
local luajava = require("luajava")
local Context = luajava.bindClass("android.content.Context")
local SpeechRecognizer = luajava.bindClass("android.speech.SpeechRecognizer")
local Intent = luajava.bindClass("android.content.Intent")
local RecognizerIntent = luajava.bindClass("android.speech.RecognizerIntent")
local Activity = luajava.bindClass("android.app.Activity")

local activity = this

-- Function to start voice recognition with Arabic language
function startVoiceRecognition(activity)

    local recognizer = SpeechRecognizer.createSpeechRecognizer(activity)
    local intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
    
    -- Set the language to Arabic
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ar-SA") -- Use "ar-SA" for Saudi Arabian Arabic, you can adjust it as needed

    local recognitionListener = luajava.createProxy("android.speech.RecognitionListener", {
        onReadyForSpeech = function(params)
            -- Handle the ready for speech event
        end,
        onResults = function(params)
            local results = params.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
            -- Handle the recognized speech results
            -- You can display or use the recognized text here
            service.setText(results.get(0)) -- Display the first recognized result in the EditText
        end,
        -- Handle other recognition events here
    })

    recognizer.setRecognitionListener(recognitionListener)
    recognizer.startListening(intent)
end

    startVoiceRecognition(activity)

-- Called when the activity is created
function onCreate(savedInstanceState)
    activity.setTitle("Voice Input Example")
end